#include "FFT_Coef_LookUp.h"
/*****************************************************************************
* Function: FFT2_Coef_LookUp()
* Purpose:  This function performs FFT2 coeffient look up table
*           table:exp(-j*2*pi*k/N),N=4096,k=0,1...,512
* Relation:
Calls       : ....
* Params:
*   <Name>          <Type>      <In/Out>    <Description>
    Coef_Re          cmplx_frN      In       output data
    Coef_Im          cmplx_frN      out      output data
    Idx              int16          In       Index to search FFT Ceof


* Return:  void
* Note:    <no>
/******************************************************************************
*  HISTORY OF CHANGES
*   <Date>          <Author>        <Version>       <DESCRIPTION>
*   2011-04-26      xubing          V1.0.0            Created
******************************************************************************/
void FFT_Coef_LookUp(fractN *Coef_Re,
                     fractN *Coef_Im,
                     int16  Idx, //Index to search FFT Ceof
                     uint8  BitLen  //比特长度
                    )
{
    int16 IdxTmp;
    IdxTmp = Idx;  //Index to search FFT Ceof
    while (IdxTmp < 0)
    {
        IdxTmp += 2048;
    }
    IdxTmp %= 2048; //ensure IdxTmp is in [0,2048)

    if (IdxTmp >= 0 && IdxTmp <= 256) //[0,pi/4),look up table directly
    {
        *Coef_Re = shr_fr1xN_Round(FFT_Coef[IdxTmp * 2], 16 - BitLen);
        *Coef_Im = shr_fr1xN_Round(FFT_Coef[IdxTmp * 2 + 1], 16 - BitLen);
    }
    else if (IdxTmp > 256 && IdxTmp < 512) //[pi/4, 2pi/4),-j*conj(exp(-j*2*pi(512-k)/N))
    {
        IdxTmp = (int16)(512 - IdxTmp);
        *Coef_Re = shr_fr1xN_Round(negate_fr1xN(FFT_Coef[IdxTmp * 2 + 1]), 16 - BitLen);
        *Coef_Im = shr_fr1xN_Round(negate_fr1xN(FFT_Coef[IdxTmp * 2]), 16 - BitLen);
    }
    else if (IdxTmp >= 512 && IdxTmp < 768) //[2pi/4,3pi/4),-j*(exp(-j*2*pi(k-512)/N))
    {
        IdxTmp = (int16)(IdxTmp - 512);
        *Coef_Re = shr_fr1xN_Round(FFT_Coef[IdxTmp * 2 + 1], 16 - BitLen);
        *Coef_Im = shr_fr1xN_Round(negate_fr1xN(FFT_Coef[IdxTmp * 2]), 16 - BitLen);
    }
    else if (IdxTmp >= 768 && IdxTmp < 1024) //[3pi/4,4pi/4),-conj((exp(-j*2*pi(1024-k)/N))
    {
        IdxTmp = (int16)(1024 - IdxTmp);
        *Coef_Re = shr_fr1xN_Round(negate_fr1xN(FFT_Coef[IdxTmp * 2]), 16 - BitLen);
        *Coef_Im = shr_fr1xN_Round(FFT_Coef[IdxTmp * 2 + 1], 16 - BitLen);
    }
    else if (IdxTmp >= 1024 && IdxTmp < 1280) //[4pi/4,5pi/4),-(exp(-j*2*pi(k-1024)/N))
    {
        IdxTmp = (int16)(IdxTmp - 1024);
        *Coef_Re = shr_fr1xN_Round(negate_fr1xN(FFT_Coef[IdxTmp * 2]), 16 - BitLen);
        *Coef_Im = shr_fr1xN_Round(negate_fr1xN(FFT_Coef[IdxTmp * 2 + 1]), 16 - BitLen);
    }
    else if (IdxTmp >= 1280 && IdxTmp < 1536) //[5pi/4,6pi/4),-j*conj(exp(-j*2*pi(1536-k)/N))
    {
        IdxTmp = (int16)(1536 - IdxTmp);
        *Coef_Re = shr_fr1xN_Round(FFT_Coef[IdxTmp * 2 + 1], 16 - BitLen);
        *Coef_Im = shr_fr1xN_Round(FFT_Coef[IdxTmp * 2], 16 - BitLen);
    }
    else if (IdxTmp >= 1536 && IdxTmp < 1792) //[6pi/4,7pi/4),j*(exp(-j*2*pi(k-1536)/N))
    {
        IdxTmp = (int16)(IdxTmp - 1536);
        *Coef_Re = shr_fr1xN_Round(negate_fr1xN(FFT_Coef[IdxTmp * 2 + 1]), 16 - BitLen);
        *Coef_Im = shr_fr1xN_Round(FFT_Coef[IdxTmp * 2], 16 - BitLen);
    }
    else if (IdxTmp >= 1792 && IdxTmp < 2048) //[7pi/4,8pi/4),conj(exp(-j*2*pi(2048-k)/N))
    {
        IdxTmp = (int16)(2048 - IdxTmp);
        *Coef_Re = shr_fr1xN_Round(FFT_Coef[IdxTmp * 2], 16 - BitLen);
        *Coef_Im = shr_fr1xN_Round(negate_fr1xN(FFT_Coef[IdxTmp * 2 + 1]), 16 - BitLen);
    }
    else
    {
        printf("Idx Error In FFT Coef LookUp function!!");
    }
}
// add, look up 12bit FFT Coef table,zhangyong@2011-05-31, CRID-Req00000026
void FFT_Coef_LookUp1(fractN *Coef_Re,
	fractN	*Coef_Im,
	int16	Idx,
	uint8  BitLen
)
{
	int16 IdxTmp;
	IdxTmp = Idx;
	if (12 == BitLen)
	{
		while (IdxTmp < 0)
		{
			IdxTmp += 2048;
		}
		IdxTmp %= 2048; //ensure IdxTmp is in [0,2048)

		if (IdxTmp >= 0 && IdxTmp <= 256)	//[0,pi/4),look up table directly
		{
			*Coef_Re = FFT_Coef_12[IdxTmp * 2];
			*Coef_Im = FFT_Coef_12[IdxTmp * 2 + 1];
		}
		else if (IdxTmp > 256 && IdxTmp < 512)//[pi/4, 2pi/4),-j*conj(exp(-j*2*pi(512-k)/N))
		{
			IdxTmp = (int16)(512 - IdxTmp);
			*Coef_Re = negate_fr1xN(FFT_Coef_12[IdxTmp * 2 + 1]);
			*Coef_Im = negate_fr1xN(FFT_Coef_12[IdxTmp * 2]);
		}
		else if (IdxTmp >= 512 && IdxTmp < 768)//[2pi/4,3pi/4),-j*(exp(-j*2*pi(k-512)/N))
		{
			IdxTmp = (int16)(IdxTmp - 512);
			*Coef_Re = FFT_Coef_12[IdxTmp * 2 + 1];
			*Coef_Im = negate_fr1xN(FFT_Coef_12[IdxTmp * 2]);

		}
		else if (IdxTmp >= 768 && IdxTmp < 1024)//[3pi/4,4pi/4),-conj((exp(-j*2*pi(1024-k)/N))
		{
			IdxTmp = (int16)(1024 - IdxTmp);
			*Coef_Re = negate_fr1xN(FFT_Coef_12[IdxTmp * 2]);
			*Coef_Im = FFT_Coef_12[IdxTmp * 2 + 1];
		}
		else if (IdxTmp >= 1024 && IdxTmp < 1280)//[4pi/4,5pi/4),-(exp(-j*2*pi(k-1024)/N))
		{
			IdxTmp = (int16)(IdxTmp - 1024);
			*Coef_Re = negate_fr1xN(FFT_Coef_12[IdxTmp * 2]);
			*Coef_Im = negate_fr1xN(FFT_Coef_12[IdxTmp * 2 + 1]);
		}
		else if (IdxTmp >= 1280 && IdxTmp < 1536)//[5pi/4,6pi/4),-j*conj(exp(-j*2*pi(1536-k)/N))
		{
			IdxTmp = (int16)(1536 - IdxTmp);
			*Coef_Re = FFT_Coef_12[IdxTmp * 2 + 1];
			*Coef_Im = FFT_Coef_12[IdxTmp * 2];

		}
		else if (IdxTmp >= 1536 && IdxTmp < 1792)//[6pi/4,7pi/4),j*(exp(-j*2*pi(k-1536)/N))
		{
			IdxTmp = (int16)(IdxTmp - 1536);
			*Coef_Re = negate_fr1xN(FFT_Coef_12[IdxTmp * 2 + 1]);
			*Coef_Im = FFT_Coef_12[IdxTmp * 2];
		}
		else if (IdxTmp >= 1792 && IdxTmp < 2048)//[7pi/4,8pi/4),conj(exp(-j*2*pi(2048-k)/N))
		{
			IdxTmp = (int16)(2048 - IdxTmp);
			*Coef_Re = FFT_Coef_12[IdxTmp * 2];
			*Coef_Im = negate_fr1xN(FFT_Coef_12[IdxTmp * 2 + 1]);
		}
		else
		{
			printf("Idx Error In FFT Coef LookUp function!!");
		}
	}
	else  //BitLen=14,FFT_Len=4096,lijianfeng add for 1861 CA 2014-12-11
	{
		while (IdxTmp < 0)
		{
			IdxTmp += 4096;
		}
		IdxTmp %= 4096; //ensure IdxTmp is in [0,2048)

		if (IdxTmp >= 0 && IdxTmp <= 512)	//[0,pi/4),look up table directly
		{
			*Coef_Re = FFT_Coef_14_4096[IdxTmp * 2];
			*Coef_Im = FFT_Coef_14_4096[IdxTmp * 2 + 1];
		}
		else if (IdxTmp > 512 && IdxTmp < 1024)//[pi/4, 2pi/4),-j*conj(exp(-j*2*pi(512-k)/N))
		{
			IdxTmp = (int16)(1024 - IdxTmp);
			*Coef_Re = negate_fr1xN(FFT_Coef_14_4096[IdxTmp * 2 + 1]);
			*Coef_Im = negate_fr1xN(FFT_Coef_14_4096[IdxTmp * 2]);
		}
		else if (IdxTmp >= 1024 && IdxTmp < 1536)//[2pi/4,3pi/4),-j*(exp(-j*2*pi(k-512)/N))
		{
			IdxTmp = (int16)(IdxTmp - 1024);
			*Coef_Re = FFT_Coef_14_4096[IdxTmp * 2 + 1];
			*Coef_Im = negate_fr1xN(FFT_Coef_14_4096[IdxTmp * 2]);

		}
		else if (IdxTmp >= 1536 && IdxTmp < 2048)//[3pi/4,4pi/4),-conj((exp(-j*2*pi(1024-k)/N))
		{
			IdxTmp = (int16)(2048 - IdxTmp);
			*Coef_Re = negate_fr1xN(FFT_Coef_14_4096[IdxTmp * 2]);
			*Coef_Im = FFT_Coef_14_4096[IdxTmp * 2 + 1];
		}
		else if (IdxTmp >= 2048 && IdxTmp < 2560)//[4pi/4,5pi/4),-(exp(-j*2*pi(k-1024)/N))
		{
			IdxTmp = (int16)(IdxTmp - 2048);
			*Coef_Re = negate_fr1xN(FFT_Coef_14_4096[IdxTmp * 2]);
			*Coef_Im = negate_fr1xN(FFT_Coef_14_4096[IdxTmp * 2 + 1]);
		}
		else if (IdxTmp >= 2560 && IdxTmp < 3072)//[5pi/4,6pi/4),-j*conj(exp(-j*2*pi(1536-k)/N))
		{
			IdxTmp = (int16)(3072 - IdxTmp);
			*Coef_Re = negate_fr1xN(FFT_Coef_14_4096[IdxTmp * 2 + 1]);
			*Coef_Im = negate_fr1xN(FFT_Coef_14_4096[IdxTmp * 2]);

		}
		else if (IdxTmp >= 3072 && IdxTmp < 3584)//[6pi/4,7pi/4),j*(exp(-j*2*pi(k-1536)/N))
		{
			IdxTmp = (int16)(IdxTmp - 3072);
			*Coef_Re = negate_fr1xN(FFT_Coef_14_4096[IdxTmp * 2 + 1]);
			*Coef_Im = FFT_Coef_14_4096[IdxTmp * 2];
		}
		else if (IdxTmp >= 3584 && IdxTmp < 4096)//[7pi/4,8pi/4),conj(exp(-j*2*pi(2048-k)/N))
		{
			IdxTmp = (int16)(4096 - IdxTmp);
			*Coef_Re = FFT_Coef_14_4096[IdxTmp * 2];
			*Coef_Im = negate_fr1xN(FFT_Coef_14_4096[IdxTmp * 2 + 1]);
		}
		else
		{
			printf("Idx Error In FFT Coef LookUp function!!");
		}
	}
}

//lijianfeng add
void FFT_Coef_LookUp2(fractN	*Coef_Re,
	fractN	*Coef_Im,
	int16	Idx,
	uint8  BitLen
)
{
	int16 IdxTmp;
	IdxTmp = Idx;
	if (BitLen == 14)
	{

		while (IdxTmp < 0)
		{
			IdxTmp += 4096;
		}
		IdxTmp %= 4096; //ensure IdxTmp is in [0,2048)

		if (IdxTmp >= 0 && IdxTmp <= 512)	//[0,pi/4),look up table directly
		{
			*Coef_Re = FFT_Coef_14_4096[IdxTmp * 2];
			*Coef_Im = FFT_Coef_14_4096[IdxTmp * 2 + 1];
		}
		else if (IdxTmp > 512 && IdxTmp < 1024)//[pi/4, 2pi/4),-j*conj(exp(-j*2*pi(512-k)/N))
		{
			IdxTmp = (int16)(1024 - IdxTmp);
			*Coef_Re = negate_fr1xN(FFT_Coef_14_4096[IdxTmp * 2 + 1]);
			*Coef_Im = negate_fr1xN(FFT_Coef_14_4096[IdxTmp * 2]);
		}
		else if (IdxTmp >= 1024 && IdxTmp < 1536)//[2pi/4,3pi/4),-j*(exp(-j*2*pi(k-512)/N))
		{
			IdxTmp = (int16)(IdxTmp - 1024);
			*Coef_Re = FFT_Coef_14_4096[IdxTmp * 2 + 1];
			*Coef_Im = negate_fr1xN(FFT_Coef_14_4096[IdxTmp * 2]);
		}
		else if (IdxTmp >= 1536 && IdxTmp < 2048)//[3pi/4,4pi/4),-conj((exp(-j*2*pi(1024-k)/N))
		{
			IdxTmp = (int16)(2048 - IdxTmp);
			*Coef_Re = negate_fr1xN(FFT_Coef_14_4096[IdxTmp * 2]);
			*Coef_Im = FFT_Coef_14_4096[IdxTmp * 2 + 1];
		}
		else if (IdxTmp >= 2048 && IdxTmp < 2560)//[4pi/4,5pi/4),-(exp(-j*2*pi(k-1024)/N))
		{
			IdxTmp = (int16)(IdxTmp - 2048);
			*Coef_Re = negate_fr1xN(FFT_Coef_14_4096[IdxTmp * 2]);
			*Coef_Im = negate_fr1xN(FFT_Coef_14_4096[IdxTmp * 2 + 1]);
		}
		else if (IdxTmp >= 2560 && IdxTmp < 3072)//[5pi/4,6pi/4),-j*conj(exp(-j*2*pi(1536-k)/N))
		{
			IdxTmp = (int16)(3072 - IdxTmp);
			*Coef_Re = FFT_Coef_14_4096[IdxTmp * 2 + 1];
			*Coef_Im = FFT_Coef_14_4096[IdxTmp * 2];
		}
		else if (IdxTmp >= 3072 && IdxTmp < 3584)//[6pi/4,7pi/4),j*(exp(-j*2*pi(k-1536)/N))
		{
			IdxTmp = (int16)(IdxTmp - 3072);
			*Coef_Re = negate_fr1xN(FFT_Coef_14_4096[IdxTmp * 2 + 1]);
			*Coef_Im = FFT_Coef_14_4096[IdxTmp * 2];
		}
		else if (IdxTmp >= 3584 && IdxTmp < 4096)//[7pi/4,8pi/4),conj(exp(-j*2*pi(2048-k)/N))
		{
			IdxTmp = (int16)(4096 - IdxTmp);
			*Coef_Re = FFT_Coef_14_4096[IdxTmp * 2], 16 - BitLen;
			*Coef_Im = negate_fr1xN(FFT_Coef_14_4096[IdxTmp * 2 + 1]);
		}
		else
		{
			printf("Idx Error In FFT Coef LookUp function!!");
		}
	}
	else if (BitLen == 16)
		// llc add 2018.07.19
	{
		while (IdxTmp < 0)
		{
			IdxTmp += 4096;
		}
		IdxTmp %= 4096; //ensure IdxTmp is in [0,2048)

		if (IdxTmp >= 0 && IdxTmp <= 512)	//[0,pi/4),look up table directly
		{
			*Coef_Re = FFT_Coef_16_4096[IdxTmp * 2];
			*Coef_Im = FFT_Coef_16_4096[IdxTmp * 2 + 1];
		}
		else if (IdxTmp > 512 && IdxTmp < 1024)//[pi/4, 2pi/4),-j*conj(exp(-j*2*pi(512-k)/N))
		{
			IdxTmp = (int16)(1024 - IdxTmp);
			*Coef_Re = negate_fr1xN(FFT_Coef_16_4096[IdxTmp * 2 + 1]);
			*Coef_Im = negate_fr1xN(FFT_Coef_16_4096[IdxTmp * 2]);
		}
		else if (IdxTmp >= 1024 && IdxTmp < 1536)//[2pi/4,3pi/4),-j*(exp(-j*2*pi(k-512)/N))
		{
			IdxTmp = (int16)(IdxTmp - 1024);
			*Coef_Re = FFT_Coef_16_4096[IdxTmp * 2 + 1];
			*Coef_Im = negate_fr1xN(FFT_Coef_16_4096[IdxTmp * 2]);
		}
		else if (IdxTmp >= 1536 && IdxTmp < 2048)//[3pi/4,4pi/4),-conj((exp(-j*2*pi(1024-k)/N))
		{
			IdxTmp = (int16)(2048 - IdxTmp);
			*Coef_Re = negate_fr1xN(FFT_Coef_16_4096[IdxTmp * 2]);
			*Coef_Im = FFT_Coef_16_4096[IdxTmp * 2 + 1];
		}
		else if (IdxTmp >= 2048 && IdxTmp < 2560)//[4pi/4,5pi/4),-(exp(-j*2*pi(k-1024)/N))
		{
			IdxTmp = (int16)(IdxTmp - 2048);
			*Coef_Re = negate_fr1xN(FFT_Coef_16_4096[IdxTmp * 2]);
			*Coef_Im = negate_fr1xN(FFT_Coef_16_4096[IdxTmp * 2 + 1]);
		}
		else if (IdxTmp >= 2560 && IdxTmp < 3072)//[5pi/4,6pi/4),-j*conj(exp(-j*2*pi(1536-k)/N))
		{
			IdxTmp = (int16)(3072 - IdxTmp);
			*Coef_Re = FFT_Coef_16_4096[IdxTmp * 2 + 1];
			*Coef_Im = FFT_Coef_16_4096[IdxTmp * 2];
		}
		else if (IdxTmp >= 3072 && IdxTmp < 3584)//[6pi/4,7pi/4),j*(exp(-j*2*pi(k-1536)/N))
		{
			IdxTmp = (int16)(IdxTmp - 3072);
			*Coef_Re = negate_fr1xN(FFT_Coef_16_4096[IdxTmp * 2 + 1]);
			*Coef_Im = FFT_Coef_16_4096[IdxTmp * 2];
		}
		else if (IdxTmp >= 3584 && IdxTmp < 4096)//[7pi/4,8pi/4),conj(exp(-j*2*pi(2048-k)/N))
		{
			IdxTmp = (int16)(4096 - IdxTmp);
			*Coef_Re = FFT_Coef_16_4096[IdxTmp * 2], 16 - BitLen;
			*Coef_Im = negate_fr1xN(FFT_Coef_16_4096[IdxTmp * 2 + 1]);
		}
		else
		{
			printf("Idx Error In FFT Coef LookUp function!!");
		}
	}
	else
	{
		printf(" Error In FFT Coef LookUp function!! no such bit");
	}
}
void FFT2_Coef_LookUp_fix
(
    fractN  *Coef_Re,
    fractN  *Coef_Im,
    int16   Idx //Index to search FFT Ceof
)
{
    int16 IdxTmp;
    //added by baiwenling, 2014-2, for 16bit, begin
    uint8 N_FFT = (uint8)BitWidth();
    //added by baiwenling, 2014-2, for 16bit, end
    if (11 == N_FFT) //added by baiwenling, 2014-2, for 16bit, end
    {
        if (Idx >= 0 && Idx <= 512) //look up table directly
        {
            *Coef_Re = (fractN)fft2_coef[Idx * 2];
            *Coef_Im = (fractN)fft2_coef[Idx * 2 + 1];
        }
        else if (Idx > 512 && Idx < 1024) //-j*conj(exp(-j*2*pi(1024-k)/N))
        {
            IdxTmp = (int16)(1024 - Idx);
            *Coef_Re = negate_fr1xN((fractN)fft2_coef[IdxTmp * 2 + 1]);
            *Coef_Im = negate_fr1xN((fractN)fft2_coef[IdxTmp * 2]);
        }
        else if (Idx >= 1024 && Idx < 1536) //-j*(exp(-j*2*pi(k-1024)/N))
        {
            IdxTmp = (int16)(Idx - 1024);
            *Coef_Re = (fractN)fft2_coef[IdxTmp * 2 + 1];
            *Coef_Im = negate_fr1xN((fractN)fft2_coef[IdxTmp * 2]);
        }
        else if (Idx >= 1536 && Idx < 2048) //-conj((exp(-j*2*pi(2048-k)/N))
        {
            IdxTmp = (int16)(2048 - Idx);
            *Coef_Re = negate_fr1xN((fractN)fft2_coef[IdxTmp * 2]);
            *Coef_Im = (fractN)fft2_coef[IdxTmp * 2 + 1];
        }
        else if (Idx >= 2048 && Idx < 2560) //-(exp(-j*2*pi(k-2048)/N))
        {
            IdxTmp = (int16)(Idx - 2048);
            *Coef_Re = negate_fr1xN((fractN)fft2_coef[IdxTmp * 2]);
            *Coef_Im = negate_fr1xN((fractN)fft2_coef[IdxTmp * 2 + 1]);
        }
        else if (Idx >= 2560 && Idx < 3072) //-j*conj(exp(-j*2*pi(3072-k)/N))
        {
            IdxTmp = (int16)(3072 - Idx);
            *Coef_Re = (fractN)fft2_coef[IdxTmp * 2 + 1];
            *Coef_Im = (fractN)fft2_coef[IdxTmp * 2];
        }
        else if (Idx >= 3072 && Idx < 3584) //j*(exp(-j*2*pi(k-3072)/N))
        {
            IdxTmp = (int16)(Idx - 3072);
            *Coef_Re = negate_fr1xN((fractN)fft2_coef[IdxTmp * 2 + 1]);
            *Coef_Im = (fractN)fft2_coef[IdxTmp * 2];
        }
        else if (Idx >= 3584 && Idx < 4096) //conj(exp(-j*2*pi(4096-k)/N))
        {
            IdxTmp = (int16)(4096 - Idx);
            *Coef_Re = (fractN)fft2_coef[IdxTmp * 2];
            *Coef_Im = negate_fr1xN((fractN)fft2_coef[IdxTmp * 2 + 1]);
        }
        else    //recursive
        {
            if (Idx < 0)
            {
                IdxTmp = Idx;
                while (IdxTmp < 0)
                {
                    IdxTmp = IdxTmp + 4096;
                }
            }
            else
            {
                IdxTmp = (int16)(Idx % 4096);
            }
            FFT2_Coef_LookUp_fix
            (
                Coef_Re,
                Coef_Im,
                IdxTmp
            );
        }
    }
    //added by baiwenling, 2014-2, for 16bit,start
    else if (16 == N_FFT)
    {
        if (Idx >= 0 && Idx <= 512) //look up table directly
        {
            *Coef_Re = (fractN)fft2_coef_bit16[Idx * 2];
            *Coef_Im = (fractN)fft2_coef_bit16[Idx * 2 + 1];
        }
        else if (Idx > 512 && Idx < 1024) //-j*conj(exp(-j*2*pi(1024-k)/N))
        {
            IdxTmp = (int16)(1024 - Idx);
            *Coef_Re = negate_fr1xN((fractN)fft2_coef_bit16[IdxTmp * 2 + 1]);
            *Coef_Im = negate_fr1xN((fractN)fft2_coef_bit16[IdxTmp * 2]);
        }
        else if (Idx >= 1024 && Idx < 1536) //-j*(exp(-j*2*pi(k-1024)/N))
        {
            IdxTmp = (int16)(Idx - 1024);
            *Coef_Re = (fractN)fft2_coef_bit16[IdxTmp * 2 + 1];
            *Coef_Im = negate_fr1xN((fractN)fft2_coef_bit16[IdxTmp * 2]);
        }
        else if (Idx >= 1536 && Idx < 2048) //-conj((exp(-j*2*pi(2048-k)/N))
        {
            IdxTmp = (int16)(2048 - Idx);
            *Coef_Re = negate_fr1xN((fractN)fft2_coef_bit16[IdxTmp * 2]);
            *Coef_Im = (fractN)fft2_coef_bit16[IdxTmp * 2 + 1];
        }
        else if (Idx >= 2048 && Idx < 2560) //-(exp(-j*2*pi(k-2048)/N))
        {
            IdxTmp = (int16)(Idx - 2048);
            *Coef_Re = negate_fr1xN((fractN)fft2_coef_bit16[IdxTmp * 2]);
            *Coef_Im = negate_fr1xN((fractN)fft2_coef_bit16[IdxTmp * 2 + 1]);
        }
        else if (Idx >= 2560 && Idx < 3072) //-j*conj(exp(-j*2*pi(3072-k)/N))
        {
            IdxTmp = (int16)(3072 - Idx);
            *Coef_Re = (fractN)fft2_coef_bit16[IdxTmp * 2 + 1];
            *Coef_Im = (fractN)fft2_coef_bit16[IdxTmp * 2];
        }
        else if (Idx >= 3072 && Idx < 3584) //j*(exp(-j*2*pi(k-3072)/N))
        {
            IdxTmp = (int16)(Idx - 3072);
            *Coef_Re = negate_fr1xN((fractN)fft2_coef_bit16[IdxTmp * 2 + 1]);
            *Coef_Im = (fractN)fft2_coef_bit16[IdxTmp * 2];
        }
        else if (Idx >= 3584 && Idx < 4096) //conj(exp(-j*2*pi(4096-k)/N))
        {
            IdxTmp = (int16)(4096 - Idx);
            *Coef_Re = (fractN)fft2_coef_bit16[IdxTmp * 2];
            *Coef_Im = negate_fr1xN((fractN)fft2_coef_bit16[IdxTmp * 2 + 1]);
        }
        else    //recursive
        {
            if (Idx < 0)
            {
                IdxTmp = Idx;
                while (IdxTmp < 0)
                {
                    IdxTmp = IdxTmp + 4096;
                }
            }
            else
            {
                IdxTmp = (int16)(Idx % 4096);
            }
            FFT2_Coef_LookUp_fix
            (
                Coef_Re,
                Coef_Im,
                IdxTmp
            );
        }
    }//else if(16==N_FFT)
    //added by baiwenling, 2014-2, for 16bit,end
}

void FFT2_Coef_LookUp4096_fix
(
	cmplx_frN phasedata[1],
	int16   Idx, //Index to search FFT Ceof,
	uint8  OffsetFlag
)
{
	int16 IdxTmp;
	fractN  Coef_Re = 0, Coef_Im = 0;
	uint8 N_FFT = (uint8)BitWidth();

	if (OffsetFlag == 1) //added by baiwenling, 2014-2, for 16bit, end
	{
		if (Idx >= 0 && Idx < 512) //look up table directly
		{
			Coef_Re = (fractN)fft2_coef_bit16_offset[Idx * 2];
			Coef_Im = (fractN)fft2_coef_bit16_offset[Idx * 2 + 1];
		}
		else if (Idx >= 512 && Idx < 1024) //-j*conj(exp(-j*2*pi(1024-k)/N))
		{
			IdxTmp = (int16)(1024 - Idx);
			Coef_Re = negate_fr1xN((fractN)fft2_coef_bit16_offset[IdxTmp * 2 + 1]);
			Coef_Im = negate_fr1xN((fractN)fft2_coef_bit16_offset[IdxTmp * 2]);
		}
		else if (Idx >= 1024 && Idx < 1536) //-j*(exp(-j*2*pi(k-1024)/N))
		{
			IdxTmp = (int16)(Idx - 1024);
			Coef_Re = (fractN)fft2_coef_bit16_offset[IdxTmp * 2 + 1];
			Coef_Im = negate_fr1xN((fractN)fft2_coef_bit16_offset[IdxTmp * 2]);
		}
		else if (Idx >= 1536 && Idx < 2048) //-conj((exp(-j*2*pi(2048-k)/N))
		{
			IdxTmp = (int16)(2048 - Idx);
			Coef_Re = negate_fr1xN((fractN)fft2_coef_bit16_offset[IdxTmp * 2]);
			Coef_Im = (fractN)fft2_coef_bit16_offset[IdxTmp * 2 + 1];
		}
		else if (Idx >= 2048 && Idx < 2560) //-(exp(-j*2*pi(k-2048)/N))
		{
			IdxTmp = (int16)(Idx - 2048);
			Coef_Re = negate_fr1xN((fractN)fft2_coef_bit16_offset[IdxTmp * 2]);
			Coef_Im = negate_fr1xN((fractN)fft2_coef_bit16_offset[IdxTmp * 2 + 1]);
		}
		else if (Idx >= 2560 && Idx < 3072) //-j*conj(exp(-j*2*pi(3072-k)/N))
		{
			IdxTmp = (int16)(3072 - Idx);
			Coef_Re = (fractN)fft2_coef_bit16_offset[IdxTmp * 2 + 1];
			Coef_Im = (fractN)fft2_coef_bit16_offset[IdxTmp * 2];
		}
		else if (Idx >= 3072 && Idx < 3584) //j*(exp(-j*2*pi(k-3072)/N))
		{
			IdxTmp = (int16)(Idx - 3072);
			Coef_Re = negate_fr1xN((fractN)fft2_coef_bit16_offset[IdxTmp * 2 + 1]);
			Coef_Im = (fractN)fft2_coef_bit16_offset[IdxTmp * 2];
		}
		else if (Idx >= 3584 && Idx < 4096) //conj(exp(-j*2*pi(4096-k)/N))
		{
			IdxTmp = (int16)(4096 - Idx);
			Coef_Re = (fractN)fft2_coef_bit16_offset[IdxTmp * 2];
			Coef_Im = negate_fr1xN((fractN)fft2_coef_bit16_offset[IdxTmp * 2 + 1]);
		}
		else    //recursive
		{
			if (Idx < 0)
			{
				IdxTmp = Idx;
				while (IdxTmp < 0)
				{
					IdxTmp = IdxTmp + 4096;
				}
			}
			else
			{
				IdxTmp = (int16)(Idx % 4096);
			}
			FFT2_Coef_LookUp4096_fix
			(
				phasedata,
				IdxTmp,
				OffsetFlag
			);
		}
	}
	else
	{
		if (Idx >= 0 && Idx <= 512) //look up table directly
		{
			Coef_Re = (fractN)fft2_coef_bit16[Idx * 2];
			Coef_Im = (fractN)fft2_coef_bit16[Idx * 2 + 1];
		}
		else if (Idx > 512 && Idx < 1024) //-j*conj(exp(-j*2*pi(1024-k)/N))
		{
			IdxTmp = (int16)(1024 - Idx);
			Coef_Re = negate_fr1xN((fractN)fft2_coef_bit16[IdxTmp * 2 + 1]);
			Coef_Im = negate_fr1xN((fractN)fft2_coef_bit16[IdxTmp * 2]);
		}
		else if (Idx >= 1024 && Idx < 1536) //-j*(exp(-j*2*pi(k-1024)/N))
		{
			IdxTmp = (int16)(Idx - 1024);
			Coef_Re = (fractN)fft2_coef_bit16[IdxTmp * 2 + 1];
			Coef_Im = negate_fr1xN((fractN)fft2_coef_bit16[IdxTmp * 2]);
		}
		else if (Idx >= 1536 && Idx < 2048) //-conj((exp(-j*2*pi(2048-k)/N))
		{
			IdxTmp = (int16)(2048 - Idx);
			Coef_Re = negate_fr1xN((fractN)fft2_coef_bit16[IdxTmp * 2]);
			Coef_Im = (fractN)fft2_coef_bit16[IdxTmp * 2 + 1];
		}
		else if (Idx >= 2048 && Idx < 2560) //-(exp(-j*2*pi(k-2048)/N))
		{
			IdxTmp = (int16)(Idx - 2048);
			Coef_Re = negate_fr1xN((fractN)fft2_coef_bit16[IdxTmp * 2]);
			Coef_Im = negate_fr1xN((fractN)fft2_coef_bit16[IdxTmp * 2 + 1]);
		}
		else if (Idx >= 2560 && Idx < 3072) //-j*conj(exp(-j*2*pi(3072-k)/N))
		{
			IdxTmp = (int16)(3072 - Idx);
			Coef_Re = (fractN)fft2_coef_bit16[IdxTmp * 2 + 1];
			Coef_Im = (fractN)fft2_coef_bit16[IdxTmp * 2];
		}
		else if (Idx >= 3072 && Idx < 3584) //j*(exp(-j*2*pi(k-3072)/N))
		{
			IdxTmp = (int16)(Idx - 3072);
			Coef_Re = negate_fr1xN((fractN)fft2_coef_bit16[IdxTmp * 2 + 1]);
			Coef_Im = (fractN)fft2_coef_bit16[IdxTmp * 2];
		}
		else if (Idx >= 3584 && Idx < 4096) //conj(exp(-j*2*pi(4096-k)/N))
		{
			IdxTmp = (int16)(4096 - Idx);
			Coef_Re = (fractN)fft2_coef_bit16[IdxTmp * 2];
			Coef_Im = negate_fr1xN((fractN)fft2_coef_bit16[IdxTmp * 2 + 1]);
		}
		else    //recursive
		{
			if (Idx < 0)
			{
				IdxTmp = Idx;
				while (IdxTmp < 0)
				{
					IdxTmp = IdxTmp + 4096;
				}
			}
			else
			{
				IdxTmp = (int16)(Idx % 4096);
			}
			FFT2_Coef_LookUp4096_fix
			(
				phasedata,
				IdxTmp,
				OffsetFlag
			);
		}
	}

	phasedata[0].re = Coef_Re;
	phasedata[0].im = Coef_Im;
}


//	%=====================================================================
//	%> @note    // exp(-j*pi/2*n/2048),n = 0,1,...,8192, Q(16,1)       
//	%> @see calls:       
//  %> @see calledby:   RotateCoefGen_fix
//	%	%> @todo            
//	%	%> @bug             
//	%> @param [in]      n
//	%> @param [out]     phasedata  
//	%> @par History
//	%> -
//	%>       Date|       Author|        Version|        DESCRIPTION
//	%> ----------|-------------|---------------|-------------------
//	%> 2021-08-24|     shanshan|          0.0.1|            created
//	%=====================================================================
void exp_1jx2xpixnvs8192_fxptable(cmplx_frN phasedata[1], int n, uint8 OffsetFlag, int  BitLength)
{
	int np;
	cmplx_frN data_table[2048];
	int i;
	int MaxProtect = (1 << (BitLength - 1)) - 1;
	int MinProtect = -1 * (1 << (BitLength - 1));
	int Re_Tmp, Im_Tmp;

	if (OffsetFlag == 0) // exp(-j*pi/2*n/2048)
	{
		for (i = 0; i < 2048; i++) // Q(BitLength, 1)
		{
			data_table[i].re = g_phase_compensation_re2048[i];
			data_table[i].im = g_phase_compensation_im2048[i];
		}
	}
	else // exp(-j*pi/2*(n+1/2)/2048)
	{
		for (i = 0; i < 2048; i++) // Q(BitLength, 1)
		{
			data_table[i].re = real_zc[i];
			data_table[i].im = imag_zc[i];
		}
	}

	if ((n >= 0) && (n < 2048)) // first quadrant
	{
		np = n;
		Re_Tmp = data_table[np].re >> (16 - BitLength);
		Im_Tmp = data_table[np].im >> (16 - BitLength);
	}
	else if ((n >= 2048) & (n < 4096)) // second quadrant
	{
		np = n - 2048;
		Re_Tmp = data_table[np].im >> (16 - BitLength);
		Im_Tmp = negate_fr1xN(data_table[np].re >> (16 - BitLength));

	}
	else if (((n >= 4096) & (n < 6144))) // third quadrant
	{
		np = n - 4096;
		Re_Tmp = negate_fr1xN(data_table[np].re >> (16 - BitLength));
		Im_Tmp = negate_fr1xN(data_table[np].im >> (16 - BitLength));
	}
	else if ((n >= 6144) & (n < 8192)) // fourth quadrant
	{
		np = n - 6144;
		Re_Tmp = negate_fr1xN(data_table[np].im >> (16 - BitLength));
		Im_Tmp = data_table[np].re >> (16 - BitLength);
	}

	//if ((n >= 0) && (n < 1024)) // 1/8
	//{
	//	np = n;
	//	Re_Tmp = data_table[np].re >> (16 - BitLength);
	//	Im_Tmp = data_table[np].im >> (16 - BitLength);
	//}
	//else if ((n >= 1024) & (n < 2048)) // 2/8
	//{
	//	//np = 2048 - n;
	//	np = 2048 - n - OffsetFlag;
	//	Re_Tmp = negate_fr1xN(data_table[np].im >> (16 - BitLength));
	//	Im_Tmp = negate_fr1xN(data_table[np].re >> (16 - BitLength));
	//}
	//else if (((n >= 2048) & (n < 3072))) // 3/8
	//{
	//	np = n - 2048;
	//	Re_Tmp = data_table[np].im >> (16 - BitLength);
	//	Im_Tmp = negate_fr1xN(data_table[np].re >> (16 - BitLength));
	//}
	//else if ((n >= 3072) & (n < 4096)) // 4/8
	//{
	//	//np = 4096 - n;
	//	np = 4096 - n - OffsetFlag;
	//	Re_Tmp = negate_fr1xN(data_table[np].re >> (16 - BitLength));
	//	Im_Tmp = data_table[np].im >> (16 - BitLength);
	//}
	//else if ((n >= 4096) & (n < 5120)) // 5/8
	//{
	//	np = n - 4096;
	//	Re_Tmp = negate_fr1xN(data_table[np].re >> (16 - BitLength));
	//	Im_Tmp = negate_fr1xN(data_table[np].im >> (16 - BitLength));
	//}
	//else if (((n >= 5120) & (n < 6144))) // 6/8
	//{
	//	//np = 6144 - n;
	//	np = 6144 - n - OffsetFlag;
	//	Re_Tmp = data_table[np].im >> (16 - BitLength);
	//	Im_Tmp = data_table[np].re >> (16 - BitLength);
	//}
	//else if ((n >= 6144) & (n < 7168)) // 7/8
	//{
	//	np = n - 6144;
	//	Re_Tmp = negate_fr1xN(data_table[np].im >> (16 - BitLength));
	//	Im_Tmp = data_table[np].re >> (16 - BitLength);
	//}
	//else
	//{
	//	//np = 8192 - n;
	//	np = 8192 - n - OffsetFlag;
	//	Re_Tmp = data_table[np].re >> (16 - BitLength);
	//	Im_Tmp = negate_fr1xN(data_table[np].im >> (16 - BitLength));
	//}

	// max protection
	if (Re_Tmp > MaxProtect)
	{
		Re_Tmp = MaxProtect;
	}
	if (Im_Tmp > MaxProtect)
	{
		Im_Tmp = MaxProtect;
	}

	// min protection
	if (Re_Tmp < MinProtect)
	{
		Re_Tmp = MinProtect;
	}
	if (Im_Tmp < MinProtect)
	{
		Im_Tmp = MinProtect;
	}

	// out
	phasedata[0].re = Re_Tmp;
	phasedata[0].im = Im_Tmp;
}
//	%=====================================================================
//	%> @note    // exp(j*pi/2*n/2048),n = 0,1,...,8192, Q(16,1)       
//	%> @see calls:       
//  %> @see calledby:   RotateCoefGen_fix
//	%	%> @todo            
//	%	%> @bug             
//	%> @param [in]      n
//	%> @param [out]     phasedata  
//	%> @par History
//	%> -
//	%>       Date|       Author|        Version|        DESCRIPTION
//	%> ----------|-------------|---------------|-------------------
//	%> 2021-08-24|     shanshan|          0.0.1|            created
//	%=====================================================================
void exp1jx2xpixnvs8192_fxptable(cmplx_frN phasedata[1], int n, uint8 OffsetFlag, int  BitLength)
{
	cmplx_frN phasedata_temp[1];
	int MaxProtect = (1 << (BitLength - 1)) - 1;
	int MinProtect = -1 * (1 << (BitLength - 1));

	// exp(-j*pi/2*n/2048),n = 0,1,...,8192, Q(16,1)
	exp_1jx2xpixnvs8192_fxptable(phasedata_temp, n, OffsetFlag, BitLength);

	// conj
	phasedata[0].re = phasedata_temp[0].re;
	phasedata[0].im = negate_fr1xN(phasedata_temp[0].im);

}

